﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalalCheck.CustomViews;
using HalalCheck.Resources;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;

namespace HalalCheck.View
{
    public class AllahNamesPage:BasePage
  {
        public AllahNamesPage()
        {
            Title = HalalStrings.AllahNamesTitle;

            var layout = new RelativeLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
            };
            //  layout.Children.Add(new TileViewHalal(), Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p => p.Bounds.Width)), Constraint.RelativeToParent((p) => p.Bounds.Height));

            var halalchecklogo = new Image
            {
                Source = "centerlogo.png"
            };
            layout.Children.Add(halalchecklogo, Constraint.RelativeToParent((p) => (p.Bounds.Width - 200) / 2), Constraint.RelativeToParent((p) => (p.Bounds.Height - 200) / 2), Constraint.RelativeToParent((p => 200)), Constraint.RelativeToParent((p) => 200));

            var listhalal = new ListView
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                SeparatorVisibility = SeparatorVisibility.Default,
                SeparatorColor = AppColors.Grey,
                BackgroundColor = Color.Transparent
            };
            listhalal.SetBinding(ListView.ItemsSourceProperty, new Binding("AllahNameList"));
            listhalal.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null)
                    return; // don't do anything if we just de-selected the row
                // do something with e.SelectedItem
                ((ListView)sender).SelectedItem = null; // de-select the row
            };
            //  _dealListView.RowHeight = (int)(Rowheight);
            listhalal.ItemTemplate = new DataTemplate(MakeItemLayout);

            listhalal.HasUnevenRows = false;
            listhalal.RowHeight = 80;
            layout.Children.Add(listhalal, Constraint.Constant(AppMargins.ItemPadding), Constraint.Constant(0), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemPadding * 2), Constraint.RelativeToParent((p) => p.Bounds.Height - AppMargins.ItemPadding * 2));
            Content = layout;
        }

        private ViewCell MakeItemLayout()
        {
            var itemLayout = new RelativeLayout
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
            };
            var background = AppViews.WhiteRounded();

            itemLayout.Children.Add(background, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p) => (p.Bounds.Width)), Constraint.RelativeToParent((p) => (p.Bounds.Height - AppMargins.ItemPadding)));     
            var halalLabel = new ExtendedLabel
            {
                XAlign = TextAlignment.Start,
                YAlign = TextAlignment.Center,
                FontAttributes = FontAttributes.None,
                FontSize = (Device.GetNamedSize(NamedSize.Large, typeof(Label))),
                TextColor = AppColors.OrigDarkBlue,
                BackgroundColor = Color.Transparent,
                //FontName = "Harabara.otf",

            };
            halalLabel.SetBinding(Label.TextProperty, new Binding("AllahName"));

            itemLayout.Children.Add(halalLabel, Constraint.Constant(AppMargins.ItemMargin*2), Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToParent((p)=>p.Bounds.Width-AppMargins.ItemMargin*2), Constraint.RelativeToView(background, (parent, s) => s.Bounds.Height - AppMargins.ItemMargin * 2));
            return new ViewCell
            {
                View = itemLayout
            };
        }
    }
}
